﻿namespace Vdc.Grid.Design
{
    using Vdc.Grid;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Globalization;

    internal class GridClientSideEventsConverter : ExpandableObjectConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                string str = (string) value;
                try
                {
                    string[] strArray = str.Split(new char[] { ',' });
                    return new GridClientSideEvents { 
                        ExposeSender = bool.Parse(strArray[0].Trim()), OnBeforeClientAdd = strArray[1].Trim(), OnBeforeClientCancelAdd = strArray[2].Trim(), OnBeforeClientCancelEdit = strArray[3].Trim(), OnBeforeClientDelete = strArray[4].Trim(), OnBeforeClientEdit = strArray[5].Trim(), OnBeforeClientInsert = strArray[6].Trim(), OnBeforeClientUpdate = strArray[7].Trim(), OnClientAdd = strArray[8].Trim(), OnClientCallback = strArray[9].Trim(), OnClientCallbackError = strArray[10].Trim(), OnClientCancelAdd = strArray[11].Trim(), OnClientCancelEdit = strArray[12].Trim(), OnClientDblClick = strArray[13].Trim(), OnClientDelete = strArray[14].Trim(), OnClientEdit = strArray[15].Trim(), 
                        OnClientExportFinish = strArray[0x10].Trim(), OnClientExportStart = strArray[0x11].Trim(), OnClientInsert = strArray[0x12].Trim(), OnClientPopulateControls = strArray[0x13].Trim(), OnClientSelect = strArray[20].Trim(), OnClientUpdate = strArray[0x15].Trim(), OnFiltering = strArray[0x16].Trim(), OnPopulatingFilterFields = strArray[0x17].Trim()
                     };
                }
                catch (Exception exception1)
                {
                    if (exception1 != null)
                    {
                    }
                }
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return "";
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
        {
            return new GridClientSideEvents { 
                ExposeSender = (bool) propertyValues["ExposeSender"], OnBeforeClientAdd = propertyValues["OnBeforeClientAdd"].ToString(), OnBeforeClientCancelAdd = propertyValues["OnBeforeClientCancelAdd"].ToString(), OnBeforeClientCancelEdit = propertyValues["OnBeforeClientCancelEdit"].ToString(), OnBeforeClientDelete = propertyValues["OnBeforeClientDelete"].ToString(), OnBeforeClientEdit = propertyValues["OnBeforeClientEdit"].ToString(), OnBeforeClientInsert = propertyValues["OnBeforeClientInsert"].ToString(), OnBeforeClientUpdate = propertyValues["OnBeforeClientUpdate"].ToString(), OnClientAdd = propertyValues["OnClientAdd"].ToString(), OnClientCallback = propertyValues["OnClientCallback"].ToString(), OnClientCallbackError = propertyValues["OnClientCallbackError"].ToString(), OnClientCancelAdd = propertyValues["OnClientCancelAdd"].ToString(), OnClientCancelEdit = propertyValues["OnClientCancelEdit"].ToString(), OnClientDblClick = propertyValues["OnClientDblClick"].ToString(), OnClientDelete = propertyValues["OnClientDelete"].ToString(), OnClientEdit = propertyValues["OnClientEdit"].ToString(), 
                OnClientExportFinish = propertyValues["OnClientExportFinish"].ToString(), OnClientExportStart = propertyValues["OnClientExportStart"].ToString(), OnClientInsert = propertyValues["OnClientInsert"].ToString(), OnClientPopulateControls = propertyValues["OnClientPopulateControls"].ToString(), OnClientSelect = propertyValues["OnClientSelect"].ToString(), OnClientUpdate = propertyValues["OnClientUpdate"].ToString(), OnFiltering = propertyValues["OnFiltering"].ToString(), OnPopulatingFilterFields = propertyValues["OnPopulatingFilterFields"].ToString()
             };
        }

        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}

